package com.accp.ssmoa.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.accp.ssmoa.dao.MenuMapper;
import com.accp.ssmoa.pojo.Menu;
import com.accp.ssmoa.pojo.MenuTree;
import com.accp.ssmoa.service.MenuService;


@Service("menuServiceimpl")
public class MenuServiceimpl implements MenuService {

	@Resource
	private MenuMapper menuDao;
	
	
	public List<Menu> getAllManu() {
		
		
		List<Menu> menus=menuDao.selectAllMenus();
		
		//调用树形菜单封装类
		MenuTree menuTree=new MenuTree();
		
		menus=menuTree.MenuList(menus);
		
		return menus;
	}


	public Menu getMenuById(Integer id) {
		
		return menuDao.selectByPrimaryKey(id);
	}


	public int addMenu(Menu menu) {
		
		return menuDao.insert(menu);
	}


	public int updateMenu(Menu menu) {
		
		return menuDao.updateByPrimaryKeySelective(menu);
	}


	public int deleteMenu(Integer id) {
		
		return menuDao.deleteByPrimaryKey(id);
	}


	public List<Object> getMenuTree() {
		List<Menu> list=menuDao.selectAllMenus();
		List<Object> listTree = new ArrayList<Object>(); 
		//字符串拼接，形成json格式树形图数据
		String str = ""; 
		        for (int i = 0; i < list.size(); i++) {  
		        	Menu  menu = list.get(i);//遍历
		       str = "{id:'" +menu.getId() + "', pId:'"+menu.getParentId()+"', name:\""+menu.getName()+"\" }";//封装ztree需要格式的字符串  
		       listTree.add(str);  
		   }

		return listTree;
	}


	public List<Menu> getMenusByRoleid(Integer roleid) {
		
		return menuDao.getMenusByRoleid(roleid);
	}

}
